package com.zhoutian.controller;

import com.zhoutian.entity.Key;
import com.zhoutian.entity.Message;
import com.zhoutian.entity.MessageRequest;
import com.zhoutian.entity.Page;
import com.zhoutian.service.SearchRecruitmentInfoService;
import com.zhoutian.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/searchRecruitmentInfo")
@CrossOrigin
public class SearchRecruitmentInfoController {

    @Autowired
    private SearchRecruitmentInfoService searchRecruitmentInfoService;

    @GetMapping("/all")
    public MessageVO allInfo() {
        MessageVO messageVO = new MessageVO();
        List<Message> messages = searchRecruitmentInfoService.findAll();
        messageVO.setMessages(messages);
        messageVO.setPageTotal(4);
        return messageVO;
    }


    @GetMapping("/info")
    public MessageVO info(Page page) {
        List<Message> messages = searchRecruitmentInfoService.findPaginated(page);
        Integer pageTotal = searchRecruitmentInfoService.findPageTotal(page);
        MessageVO messageVO = new MessageVO();
        messageVO.setMessages(messages)
                .setPageTotal(pageTotal);
        return messageVO;
    }

    // 将点击搜索后传过来的链接进行分类
    @GetMapping("/search")
    public MessageVO search (MessageRequest messageRequest) {
        if ("".equals(messageRequest.getKey())) {
            Page page = new Page();
            page.setPageSize(messageRequest.getPageSize())
                    .setPageNum(messageRequest.getPageNum());
            return info(page);
        } else {
            return searchByKey(messageRequest);
        }
    }

    public MessageVO searchByKey (MessageRequest messageRequest) {
        log.info("messageRequest: [{}]", messageRequest);
        MessageVO messageVO = searchRecruitmentInfoService.findByKey(messageRequest);
        log.info("messageVO: [{}]", messageVO);
        return messageVO;
    }
}
